/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PantallaPruebas.java
 *
 * Created on 30-abr-2012, 23:56:21
 */

package edu.uoc.tds.pec4.swingx;

import edu.uoc.tds.pec4.common.RemotoInterface;
import edu.uoc.tds.pec4.i18n.GestorIdiomas;
import edu.uoc.tds.pec4.model.Prueba;
import edu.uoc.tds.pec4.model.TipoPrueba;
import edu.uoc.tds.pec4.model.Usuario;
import edu.uoc.tds.pec4.server.bbdd.GestorParametro;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Fernando
 */
public class PantallaPruebas extends javax.swing.JDialog {
    private Usuario usuario;
    private boolean editable;
    private RemotoInterface remoto;
    private boolean insertandoRegistro;
    private int id_prueba;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    /** Creates new form PantallaPruebas */
    public PantallaPruebas(PantallaMenuPrincipal parent, boolean modal) throws RemoteException {
        super(parent, modal);
        this.remoto = parent.getRemoto();
        this.usuario = parent.getUsuario();
        initComponents();
        myInitComponents();
    }

    public RemotoInterface getRemoto() {
        return this.remoto;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBarraHerramientas = new javax.swing.JToolBar();
        btnNew = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        btnFind = new javax.swing.JButton();
        jSeparator6 = new javax.swing.JToolBar.Separator();
        btnFirst = new javax.swing.JButton();
        btnPrevious = new javax.swing.JButton();
        btnForward = new javax.swing.JButton();
        btnLast = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        btnExit = new javax.swing.JButton();
        jPanelPruebas = new javax.swing.JPanel();
        jlblFechaAlta = new javax.swing.JLabel();
        jtxtFechaAlta = new javax.swing.JTextField();
        jlblFechaModificacion = new javax.swing.JLabel();
        jtxtFechaModificacion = new javax.swing.JTextField();
        jtxtFechaBaja = new javax.swing.JTextField();
        jlblFechaBaja = new javax.swing.JLabel();
        jlblFechaDescripcion = new javax.swing.JLabel();
        jtxtDescripcion = new javax.swing.JTextField();
        jlblNumeroDePlazas = new javax.swing.JLabel();
        jtxtNumeroPlazas = new javax.swing.JTextField();
        jtxtResponsableDeAlta = new javax.swing.JTextField();
        jlblResponsableDeAlta = new javax.swing.JLabel();
        jtxtEstadoDeLaPrueba = new javax.swing.JTextField();
        jlblEstadoDeLaPrueba = new javax.swing.JLabel();
        jlblFechasDeInscripcion = new javax.swing.JLabel();
        jlblDesde = new javax.swing.JLabel();
        jtxtInscripcionDesde = new javax.swing.JTextField();
        jlblHasta = new javax.swing.JLabel();
        jtxtInscripcionHasta = new javax.swing.JTextField();
        jbtnRondaPreliminar = new javax.swing.JButton();
        jbtnRondaFinal = new javax.swing.JButton();
        jlblId = new javax.swing.JLabel();
        jtxtId = new javax.swing.JTextField();
        jmnuBarPruebas = new javax.swing.JMenuBar();
        jmnuArchivo = new javax.swing.JMenu();
        jmnuNuevo = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jmnuSalir = new javax.swing.JMenuItem();
        jmnuEdicion = new javax.swing.JMenu();
        jmnuEditar = new javax.swing.JCheckBoxMenuItem();
        jmnuAyuda = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("eOLympics - Pruebas");

        jToolBarraHerramientas.setRollover(true);

        btnNew.setForeground(new java.awt.Color(102, 153, 255));
        btnNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/New document.png"))); // NOI18N
        btnNew.setToolTipText("Nuevo");
        btnNew.setFocusable(false);
        btnNew.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNew.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });
        jToolBarraHerramientas.add(btnNew);
        jToolBarraHerramientas.add(jSeparator2);

        btnEdit.setForeground(new java.awt.Color(102, 153, 255));
        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Modify.png"))); // NOI18N
        btnEdit.setToolTipText("Moificar");
        btnEdit.setFocusable(false);
        btnEdit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEdit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        jToolBarraHerramientas.add(btnEdit);

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Delete.png"))); // NOI18N
        btnDelete.setToolTipText("Eliminar");
        btnDelete.setFocusable(false);
        btnDelete.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDelete.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        jToolBarraHerramientas.add(btnDelete);

        btnSave.setForeground(new java.awt.Color(102, 153, 255));
        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Download.png"))); // NOI18N
        btnSave.setToolTipText("Guardar");
        btnSave.setFocusable(false);
        btnSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jToolBarraHerramientas.add(btnSave);
        jToolBarraHerramientas.add(jSeparator4);

        btnFind.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Find.png"))); // NOI18N
        btnFind.setToolTipText("Buscar");
        btnFind.setFocusable(false);
        btnFind.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnFind.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnFind.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFindActionPerformed(evt);
            }
        });
        jToolBarraHerramientas.add(btnFind);
        jToolBarraHerramientas.add(jSeparator6);

        btnFirst.setForeground(new java.awt.Color(102, 153, 255));
        btnFirst.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/First record.png"))); // NOI18N
        btnFirst.setToolTipText("Primero");
        btnFirst.setEnabled(false);
        btnFirst.setFocusable(false);
        btnFirst.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnFirst.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarraHerramientas.add(btnFirst);

        btnPrevious.setForeground(new java.awt.Color(102, 153, 255));
        btnPrevious.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Playback.png"))); // NOI18N
        btnPrevious.setToolTipText("Anterior");
        btnPrevious.setEnabled(false);
        btnPrevious.setFocusable(false);
        btnPrevious.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnPrevious.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarraHerramientas.add(btnPrevious);

        btnForward.setForeground(new java.awt.Color(102, 153, 255));
        btnForward.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Play.png"))); // NOI18N
        btnForward.setToolTipText("Posterior");
        btnForward.setEnabled(false);
        btnForward.setFocusable(false);
        btnForward.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnForward.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarraHerramientas.add(btnForward);

        btnLast.setForeground(new java.awt.Color(102, 153, 255));
        btnLast.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Last recor.png"))); // NOI18N
        btnLast.setToolTipText("Ultimo");
        btnLast.setEnabled(false);
        btnLast.setFocusable(false);
        btnLast.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnLast.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarraHerramientas.add(btnLast);
        jToolBarraHerramientas.add(jSeparator5);

        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Exit.png"))); // NOI18N
        btnExit.setToolTipText("Salir");
        btnExit.setFocusable(false);
        btnExit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnExit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitsalir(evt);
            }
        });
        jToolBarraHerramientas.add(btnExit);

        jlblFechaAlta.setText("Fecha Alta");

        jtxtFechaAlta.setEditable(false);

        jlblFechaModificacion.setText("Fecha Modificación");

        jtxtFechaModificacion.setEditable(false);

        jtxtFechaBaja.setEditable(false);

        jlblFechaBaja.setText("Fecha Baja");

        jlblFechaDescripcion.setText("Descripción");

        jlblNumeroDePlazas.setText("Nº de plazas");

        jtxtResponsableDeAlta.setEditable(false);

        jlblResponsableDeAlta.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jlblResponsableDeAlta.setText("Responsable de Alta");

        jtxtEstadoDeLaPrueba.setEditable(false);

        jlblEstadoDeLaPrueba.setFont(new java.awt.Font("Tahoma", 1, 11));
        jlblEstadoDeLaPrueba.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jlblEstadoDeLaPrueba.setText("Estado de la Prueba");

        jlblFechasDeInscripcion.setFont(new java.awt.Font("Tahoma", 1, 11));
        jlblFechasDeInscripcion.setText("Fechas de Inscripción");

        jlblDesde.setText("Desde (DD/MM/AAAA)");

        jlblHasta.setText("Hasta (DD/MM/AAAA)");

        jbtnRondaPreliminar.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jbtnRondaPreliminar.setText("Ronda Preliminar");
        jbtnRondaPreliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRondaPreliminarActionPerformed(evt);
            }
        });

        jbtnRondaFinal.setFont(new java.awt.Font("Tahoma", 0, 12));
        jbtnRondaFinal.setText("Ronda Final");
        jbtnRondaFinal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRondaFinalActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelPruebasLayout = new javax.swing.GroupLayout(jPanelPruebas);
        jPanelPruebas.setLayout(jPanelPruebasLayout);
        jPanelPruebasLayout.setHorizontalGroup(
            jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPruebasLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlblNumeroDePlazas, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanelPruebasLayout.createSequentialGroup()
                        .addComponent(jlblFechaAlta)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtxtFechaAlta, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jlblFechaModificacion)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtxtFechaModificacion, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jlblFechaBaja)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxtFechaBaja, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(80, 80, 80))
                    .addGroup(jPanelPruebasLayout.createSequentialGroup()
                        .addComponent(jlblFechaDescripcion)
                        .addGap(18, 18, 18)
                        .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelPruebasLayout.createSequentialGroup()
                                .addComponent(jtxtNumeroPlazas, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(34, 34, 34)
                                .addComponent(jlblResponsableDeAlta, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jtxtResponsableDeAlta, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))
                            .addComponent(jtxtDescripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE))
                        .addGap(79, 79, 79))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelPruebasLayout.createSequentialGroup()
                        .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelPruebasLayout.createSequentialGroup()
                                .addComponent(jlblDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jtxtInscripcionDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jlblFechasDeInscripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                            .addGroup(jPanelPruebasLayout.createSequentialGroup()
                                .addComponent(jlblHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jtxtInscripcionHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(66, 66, 66)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelPruebasLayout.createSequentialGroup()
                                .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jtxtEstadoDeLaPrueba, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                                    .addComponent(jbtnRondaFinal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                                    .addComponent(jbtnRondaPreliminar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
                                .addGap(80, 80, 80))
                            .addComponent(jlblEstadoDeLaPrueba, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanelPruebasLayout.setVerticalGroup(
            jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPruebasLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblFechaAlta)
                    .addComponent(jlblFechaModificacion)
                    .addComponent(jtxtFechaAlta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtFechaModificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblFechaBaja)
                    .addComponent(jtxtFechaBaja, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblFechaDescripcion)
                    .addComponent(jtxtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblNumeroDePlazas)
                    .addComponent(jtxtNumeroPlazas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblResponsableDeAlta)
                    .addComponent(jtxtResponsableDeAlta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addComponent(jlblEstadoDeLaPrueba)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelPruebasLayout.createSequentialGroup()
                        .addComponent(jtxtEstadoDeLaPrueba, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnRondaPreliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnRondaFinal, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelPruebasLayout.createSequentialGroup()
                        .addComponent(jlblFechasDeInscripcion)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jlblDesde)
                            .addComponent(jtxtInscripcionDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelPruebasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jlblHasta)
                            .addComponent(jtxtInscripcionHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jlblId.setText("Id");

        jtxtId.setEditable(false);
        jtxtId.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jmnuArchivo.setText("Archivo");
        jmnuArchivo.setFont(new java.awt.Font("Tahoma", 1, 11));

        jmnuNuevo.setText("Nuevo");
        jmnuNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmnuNuevoActionPerformed(evt);
            }
        });
        jmnuArchivo.add(jmnuNuevo);
        jmnuArchivo.add(jSeparator1);

        jmnuSalir.setText("Salir");
        jmnuArchivo.add(jmnuSalir);

        jmnuBarPruebas.add(jmnuArchivo);

        jmnuEdicion.setText("Edición");
        jmnuEdicion.setFont(new java.awt.Font("Tahoma", 1, 11));

        jmnuEditar.setText("Editar");
        jmnuEditar.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jmnuEditarItemStateChanged(evt);
            }
        });
        jmnuEdicion.add(jmnuEditar);

        jmnuBarPruebas.add(jmnuEdicion);

        jmnuAyuda.setText("Ayuda");
        jmnuAyuda.setFont(new java.awt.Font("Tahoma", 1, 11));
        jmnuBarPruebas.add(jmnuAyuda);

        setJMenuBar(jmnuBarPruebas);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBarraHerramientas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
                .addComponent(jlblId)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtId, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelPruebas, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBarraHerramientas, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jlblId)
                            .addComponent(jtxtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(jPanelPruebas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        // TODO add your handling code here:
        if(this.editable)
            this.addPrueba();
}//GEN-LAST:event_btnNewActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        this.setEditable(!this.editable);
}//GEN-LAST:event_btnEditActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_btnDeleteActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        this.insertarPrueba();
}//GEN-LAST:event_btnSaveActionPerformed

    private void btnFindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFindActionPerformed
        // TODO add your handling code here:
        this.abrirPantallaPruebasLista();
}//GEN-LAST:event_btnFindActionPerformed

    private void btnExitsalir(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitsalir
        // TODO add your handling code here:
        this.setVisible(false);
}//GEN-LAST:event_btnExitsalir

    private void jmnuEditarItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jmnuEditarItemStateChanged
        // TODO add your handling code here:
        this.editable = !this.editable;
        this.setEditable(editable);
    }//GEN-LAST:event_jmnuEditarItemStateChanged

    private void jmnuNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmnuNuevoActionPerformed
        // TODO add your handling code here:
        this.editable = true;
        this.setEditable(this.editable);
        this.addPrueba();;
    }//GEN-LAST:event_jmnuNuevoActionPerformed

    private void jbtnRondaPreliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRondaPreliminarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jbtnRondaPreliminarActionPerformed

    private void jbtnRondaFinalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRondaFinalActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jbtnRondaFinalActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PantallaPruebas dialog = null;
                try {
                    dialog = new PantallaPruebas(new PantallaMenuPrincipal(), true);
                } catch (RemoteException ex) {
                    Logger.getLogger(PantallaPruebas.class.getName()).log(Level.SEVERE, null, ex);
                }

                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    private void setEditable(boolean editable){
        this.editable = editable;

        this.jtxtDescripcion.setEditable(this.editable);
        this.jtxtNumeroPlazas.setEditable(this.editable);
        this.jtxtInscripcionDesde.setEditable(this.editable);
        this.jtxtInscripcionHasta.setEditable(this.editable);

        this.jbtnRondaPreliminar.setEnabled(!editable);
        this.jbtnRondaFinal.setEnabled(!editable);
    }

    private void limpiarPantalla(){
        this.jtxtId.setText(null);
        this.jtxtDescripcion.setText(null);
        this.jtxtFechaAlta.setText(null);
        this.jtxtFechaModificacion.setText(null);
        this.jtxtFechaBaja.setText(null);
        this.jtxtNumeroPlazas.setText(null);
        this.jtxtInscripcionDesde.setText(null);
        this.jtxtInscripcionHasta.setText(null);
        this.jtxtResponsableDeAlta.setText(null);
        this.jtxtEstadoDeLaPrueba.setText(null);
    }

    private void abrirPantallaPruebasLista(){
        PantallaPruebasLista pantallaPruebasLista = new PantallaPruebasLista(this,true);
        pantallaPruebasLista.setVisible(true);
    }

    //Se accede desde un JDialog
    protected void mostrarRegistro(int id_prueba) throws RemoteException{
        //Si refrescamos pantalla ya no estaremos añadiendo
        this.insertandoRegistro = false;
        this.limpiarPantalla();

        Prueba prueba = null;
        prueba = this.remoto.getPrueba(id_prueba);

        this.id_prueba = prueba.getId_prueba();

        if(prueba != null){
            this.jtxtId.setText(String.valueOf(id_prueba));
            this.jtxtDescripcion.setText(prueba.getDescripcion_prueba());
            this.jtxtFechaAlta.setText(getFormatoFecha(prueba.getFecha_alta()));
            this.jtxtFechaModificacion.setText(getFormatoFecha(prueba.getFecha_modificacion()));
            this.jtxtFechaBaja.setText(getFormatoFecha(prueba.getFecha_baja()));
            this.jtxtNumeroPlazas.setText(String.valueOf(prueba.getNumero_plazas()));
            this.jtxtInscripcionDesde.setText(getFormatoFecha(prueba.getFecha_inicio_inscripcion()));
            this.jtxtInscripcionHasta.setText(getFormatoFecha(prueba.getFecha_fin_inscripcion()));
            this.jtxtResponsableDeAlta.setText(this.getNombreCompletoUsuario(prueba.getId_responsable_alta()));
            this.jtxtEstadoDeLaPrueba.setText(this.getDescripcionEstadoPrueba(prueba.getId_estado_prueba()));
        }

        this.verificarSiRondaEliminatoria();
    }

    private void verificarSiRondaEliminatoria(){
        int numeroDePlazas;
        int numeroDeParticipantesEnFinal;

        numeroDePlazas = Integer.parseInt(this.jtxtNumeroPlazas.getText());
        GestorParametro gestorParametro = new GestorParametro();
        numeroDeParticipantesEnFinal = gestorParametro.numeroPersonasMaximoEnFinal();

        if (numeroDePlazas > numeroDeParticipantesEnFinal) {
            this.jbtnRondaPreliminar.setEnabled(true);
        }
        else{
            this.jbtnRondaPreliminar.setEnabled(false);
        }
    }

    private String getDescripcionEstadoPrueba(int id_estado_prueba){
        TipoPrueba tipoPrueba[] = TipoPrueba.values();
        return tipoPrueba[id_estado_prueba].toString();
    }

    private String getNombreCompletoUsuario(int id_usuario){
        System.out.println(id_usuario);
        Usuario usuario = null;
        StringBuffer nombreCompleto = new StringBuffer();

        try{
            usuario = this.remoto.getUsuario(id_usuario);
            nombreCompleto.append(usuario.getNombre());
            nombreCompleto.append(" ");
            nombreCompleto.append(usuario.getApellido_primero());
            nombreCompleto.append(" ");
            nombreCompleto.append(usuario.getApellido_segundo());
        }

        catch (RemoteException exc){
            JOptionPane.showMessageDialog(null, exc.getMessage(), GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
        }

        catch (Exception exc) {
            JOptionPane.showMessageDialog(null, exc.getMessage(), GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
        }

        finally{
            return nombreCompleto.toString();
        }
    }

    private void addPrueba(){
        GestorParametro gestorParametro = new GestorParametro();

        this.insertandoRegistro = true;
        this.limpiarPantalla();

        //ValoresPredeterminados
        this.jtxtFechaAlta.setText(this.getFormatoFecha(new Date()));
        this.jtxtEstadoDeLaPrueba.setText(this.getDescripcionEstadoPrueba(TipoPrueba.INSCRIPCION.valor()));

        this.jtxtNumeroPlazas.setText(String.valueOf(gestorParametro.getNumeroPersonasMaximoPorPrueba()));
        this.jtxtResponsableDeAlta.setText(this.getNombreCompletoUsuario(this.usuario.getId_usuario()));
    }

    private void insertarPrueba(){
        if(this.insertandoRegistro){
            Prueba prueba = null;
            try {
                prueba = this.convertirAObjeto();

                if (this.remoto.validarPrueba(prueba)) {
                    this.remoto.insertarPrueba(prueba);
                    JOptionPane.showMessageDialog(null, "Prueba insertada satisfactoriamente", GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
                    
                    this.insertandoRegistro = false;
                    this.editable = false;
                    this.setEditable(false);
                    this.verificarSiRondaEliminatoria();
                }
            }

            catch (RemoteException exc) {
                JOptionPane.showMessageDialog(null, exc.getMessage(), GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
            }
            
            catch (Exception exc) {
                JOptionPane.showMessageDialog(null, exc.getMessage(), GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
            }
        }
        else {
            JOptionPane.showMessageDialog(null, "Por especificacion del proyecto no se pueden modificar registros.", GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
            try {
                this.mostrarRegistro(Integer.valueOf(this.jtxtId.getText()));
            }

            catch (RemoteException ex) {
                JOptionPane.showMessageDialog(null, "Error al mostrar la prueba.", GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public Prueba convertirAObjeto(){
        Prueba prueba = new Prueba();

        try {
            prueba.setDescripcion_prueba(this.jtxtDescripcion.getText());
            prueba.setFecha_alta(this.getFormatoFecha(this.jtxtFechaAlta.getText()));
            //prueba.setFecha_modificacion();
            //prueba.setFecha_baja();
            prueba.setNumero_plazas(this.getNumero(this.jtxtNumeroPlazas.getText()));
            prueba.setFecha_inicio_inscripcion(this.getFormatoFecha(this.jtxtInscripcionDesde.getText()));
            prueba.setFecha_fin_inscripcion(this.getFormatoFecha(this.jtxtInscripcionHasta.getText()));
            prueba.setId_estado_prueba(TipoPrueba.INSCRIPCION.valor());
            prueba.setId_responsable_alta(this.usuario.getId_usuario());

            System.out.println("FII: " + prueba.getFecha_inicio_inscripcion());
            System.out.println("FFI: " + prueba.getFecha_fin_inscripcion());
            System.out.println("Estado: " + prueba.getId_estado_prueba());
        }

        catch (Exception exc) {
            JOptionPane.showMessageDialog(null, "Error al validar los campos.", GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
        }

        return prueba;
    }

    private String getFormatoFecha(Date fecha){
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

        if(fecha == null)
            return null;

        return sdf.format(fecha);
    }

    private Date getFormatoFecha(String fechaTexto){
        Date fecha = new Date();
        try {
            if (!fechaTexto.isEmpty()) {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                fecha = sdf.parse(fechaTexto);
            }
        }

        catch (ParseException ex) {
            JOptionPane.showMessageDialog(null, "Error al convertir a fecha.", GestorIdiomas.getMessage("error"), JOptionPane.ERROR_MESSAGE);
        }

        return fecha;
    }

    private int getNumero(String cadena){
        if(cadena.isEmpty())
            return 0;

        else
            return Integer.parseInt(cadena);
    }

    private void myInitComponents() throws RemoteException{
        this.setEditable(false);
        this.mostrarRegistro(1);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnFind;
    private javax.swing.JButton btnFirst;
    private javax.swing.JButton btnForward;
    private javax.swing.JButton btnLast;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnPrevious;
    private javax.swing.JButton btnSave;
    private javax.swing.JPanel jPanelPruebas;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JToolBar.Separator jSeparator6;
    private javax.swing.JToolBar jToolBarraHerramientas;
    private javax.swing.JButton jbtnRondaFinal;
    private javax.swing.JButton jbtnRondaPreliminar;
    private javax.swing.JLabel jlblDesde;
    private javax.swing.JLabel jlblEstadoDeLaPrueba;
    private javax.swing.JLabel jlblFechaAlta;
    private javax.swing.JLabel jlblFechaBaja;
    private javax.swing.JLabel jlblFechaDescripcion;
    private javax.swing.JLabel jlblFechaModificacion;
    private javax.swing.JLabel jlblFechasDeInscripcion;
    private javax.swing.JLabel jlblHasta;
    private javax.swing.JLabel jlblId;
    private javax.swing.JLabel jlblNumeroDePlazas;
    private javax.swing.JLabel jlblResponsableDeAlta;
    private javax.swing.JMenu jmnuArchivo;
    private javax.swing.JMenu jmnuAyuda;
    private javax.swing.JMenuBar jmnuBarPruebas;
    private javax.swing.JMenu jmnuEdicion;
    private javax.swing.JCheckBoxMenuItem jmnuEditar;
    private javax.swing.JMenuItem jmnuNuevo;
    private javax.swing.JMenuItem jmnuSalir;
    private javax.swing.JTextField jtxtDescripcion;
    private javax.swing.JTextField jtxtEstadoDeLaPrueba;
    private javax.swing.JTextField jtxtFechaAlta;
    private javax.swing.JTextField jtxtFechaBaja;
    private javax.swing.JTextField jtxtFechaModificacion;
    private javax.swing.JTextField jtxtId;
    private javax.swing.JTextField jtxtInscripcionDesde;
    private javax.swing.JTextField jtxtInscripcionHasta;
    private javax.swing.JTextField jtxtNumeroPlazas;
    private javax.swing.JTextField jtxtResponsableDeAlta;
    // End of variables declaration//GEN-END:variables
}
